# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
### Reducing Prejudice and Support for Religious 
### Nationalism Through Conversations on WhatsApp 

### Author: Rajeshwari Majumdar
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# File:     supp_hte.R
# Purpose:  analyze various HTEs for the main prejudice outcome; 
#           produce Figures E.1 and E.2
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Load data 
rm(list=ls())
load("data/Completes_Merged_Anonymized.RData")

# Functions for subgroup analyses ---- 

# Basic regression function, extract coefficients
fn_reg_basic = function(data, depvars, indepvar, who) {
  if (who == "Hindus") {
    data = data %>% filter(religion == "Hindu")
  } else if (who == "Muslims"){
    data = data %>% filter(religion == "Muslim")
  }
  results = lapply(depvars, function(depvar) {
    model = lm(as.formula(paste(depvar, "~", indepvar)), data = data)
    tidy(model)
  })
  bind_rows(results)
}

# Use previous function to run regressions by subgroups
fn_subgroup = function(data, condition, label) {
  fn_reg_basic(
    data %>% filter({{ condition }}),
    "post.feelth_muslim",
    "condition_pair",
    "Hindus"
  ) %>%
    mutate(subgroup = label)
}

# Individual-level variables ----

## Figure E.1: Heterogeneous treatment effects on short-term feelings about Muslims by individual-level characteristics ----

# Estimate models for panel (a) Demographics 
out = bind_rows(
  # Gender
  fn_subgroup(d, gender_f == 0, "Male"),
  fn_subgroup(d, gender_f == 1, "Female"),
  # Education
  fn_subgroup(d, college == 0, "Not College Graduate"),
  fn_subgroup(d, college == 1, "College Graduate"),
  # Age
  fn_subgroup(d, age <= median(d$age), "Below Median Age"),
  fn_subgroup(d, age > median(d$age), "Above Median Age"),
  # Income
  fn_subgroup(d, incomeamnt <= median(d$incomeamnt), "Below Median Income"),
  fn_subgroup(d, incomeamnt > median(d$incomeamnt), "Above Median Income"),
  # Caste
  fn_subgroup(d, castefw == 0, "SC/ST/OBC"),
  fn_subgroup(d, castefw == 1, "Forward Caste"),
  # Region
  fn_subgroup(d, state_zonal %in% c("North","Central"), "Region: North/Central"),
  fn_subgroup(d, state_zonal %in% c("East","West","South"), "Region: East/West/South")
)
out = subset(out, term!="(Intercept)")
out$subgroup = factor(out$subgroup, levels = c("Male","Female","College Graduate","Not College Graduate",
                                               "Above Median Age","Below Median Age","Above Median Income","Below Median Income",
                                               "SC/ST/OBC","Forward Caste","Region: North/Central","Region: East/West/South"))
out$subgroup = forcats::fct_rev(out$subgroup)


# Estimate models for panel (b) Preferences, dispositions, experiences
out = bind_rows(
  # Prejudice
  fn_subgroup(d, prejudice_binary == 0, "Baseline prejudice = 0"),
  fn_subgroup(d, prejudice_binary == 1, "Baseline prejudice = 1"),
  # BJP support
  fn_subgroup(d, supportBJP <= 3, "BJP Support: Weak/None"),
  fn_subgroup(d, supportBJP >= 4, "BJP Support: Strong"),
  # Religiosity
  fn_subgroup(d, religiosity_score < 3, "Religiosity: Low"),
  fn_subgroup(d, religiosity_score >= 3, "Religiosity: High"),
  # Empathy
  fn_subgroup(d, ECscore <= median(d$ECscore), "Below Median Empathetic Concern Score"),
  fn_subgroup(d, ECscore > median(d$ECscore), "Above Median Empathetic Concern Score"),
  fn_subgroup(d, PTscore <= median(d$PTscore), "Below Median Perspective Taking Score"),
  fn_subgroup(d, PTscore > median(d$PTscore), "Above Median Perspective Taking Score"),
  # Prior experiences
  fn_subgroup(d, hasfriend_muslim == 0, "Has Muslim Friend: No"),
  fn_subgroup(d, hasfriend_muslim == 1, "Has Muslim Friend: Yes"),
  fn_subgroup(d, underwent.religious == 0, "Experienced Religious Discrim./Viol.: No"),
  fn_subgroup(d, underwent.religious == 1, "Experienced Religious Discrim./Viol.: Yes"),
  # Tendency to write more
  fn_subgroup(d, nchar_initial <= median(d$nchar_initial, na.rm = TRUE), "Below Median Initial Response Length"),
  fn_subgroup(d, nchar_initial > median(d$nchar_initial, na.rm = TRUE), "Above Median Initial Response Length"),
  # Interest in politics
  fn_subgroup(d, political_interest == "Not" | political_interest == "Somewhat", "Not Very Interested In Politics"),
  fn_subgroup(d, political_interest == "Very", "Very Interested In Politics"),
  # Social media usage index
  fn_subgroup(d, socialmedia_score <= median(d$socialmedia_score, na.rm = TRUE), "Below Median Social Media Usage"),
  fn_subgroup(d, socialmedia_score > median(d$socialmedia_score, na.rm = TRUE), "Above Median Social Media Usage")
)
out = subset(out, term!="(Intercept)")
out$subgroup = factor(out$subgroup, levels = c("Baseline prejudice = 0","Baseline prejudice = 1","BJP Support: Weak/None","BJP Support: Strong",
                                               "Religiosity: Low","Religiosity: High",
                                               "Has Muslim Friend: Yes","Has Muslim Friend: No",            
                                               "Experienced Religious Discrim./Viol.: Yes","Experienced Religious Discrim./Viol.: No", 
                                               "Very Interested In Politics","Not Very Interested In Politics","Below Median Initial Response Length","Above Median Initial Response Length",
                                               "Below Median Social Media Usage","Above Median Social Media Usage",
                                               "Below Median Empathetic Concern Score","Above Median Empathetic Concern Score","Below Median Perspective Taking Score","Above Median Perspective Taking Score"))
out$subgroup = forcats::fct_rev(out$subgroup)


# Plot 
ggplot(out, aes(x = estimate, y = subgroup)) +
  geom_point(position = position_dodge(.6)) +
  geom_errorbar(aes(xmin = estimate - std.error, xmax = estimate + std.error), width = 0.1, position = position_dodge(.6)) +
  scale_x_continuous(limits = c(0,0.8), breaks = seq(0,0.8,0.1)) +
  geom_vline(xintercept = 0.40197, color = "blue", lty = 2) + 
  labs(x = expression(Coefficient~estimate~of~italic("Hindu-Muslim pair")), y = "Subgroup") +
  theme_classic(base_size = 15) 
# note: 0.40197 is the ATE (no controls included)


# Pair-level variables ----
## Figure E.2: Heterogeneous treatment effects on short-term feelings about Muslims by pair-level characteristics ----
out = bind_rows(
  # BJP support gap
  fn_subgroup(d, pair_gap_supportBJP >= 2, "Incongruent Party Support"),
  fn_subgroup(d, pair_gap_supportBJP < 2, "Congruent Party Support"),
  # Education gap
  fn_subgroup(d, pair_gap_education > 0, "Different Education Level"),
  fn_subgroup(d, pair_gap_education == 0, "Same Education Level"),
  # Language gap
  fn_subgroup(d, pair_gap_languageSame == 0, "Different Language"),
  fn_subgroup(d, pair_gap_languageSame == 1, "Same Language"),
  # Word count
  fn_subgroup(d, chat_wordcount_total <= median(d$chat_wordcount_total), "Below Median Word Count"),
  fn_subgroup(d, chat_wordcount_total > median(d$chat_wordcount_total), "Above Median Word Count"),
  # Communication
  fn_subgroup(d, exper2_partnerdiffic == "No", "No Difficulty Understanding"),
  fn_subgroup(d, exper2_partnerdiffic == "Yes", "Some Difficulty Understanding")
)
out = subset(out, term!="(Intercept)")
out$subgroup = factor(out$subgroup, levels = c("Below Median Word Count","Above Median Word Count",
                                                 "Congruent Party Support","Incongruent Party Support",
                                                 "Same Education Level","Different Education Level","Same Language","Different Language",
                                                 "No Difficulty Understanding","Some Difficulty Understanding"))
out$subgroup = forcats::fct_rev(out$subgroup)


# Plot 
ggplot(out, aes(x = estimate, y = subgroup)) +
  geom_point(position = position_dodge(.6)) +
  geom_errorbar(aes(xmin = estimate - std.error, xmax = estimate + std.error), width = 0.1, position = position_dodge(.6)) +
  scale_x_continuous(limits = c(0,0.8), breaks = seq(0,0.8,0.1)) +
  geom_vline(xintercept = 0.40197, color = "blue", lty = 2) + 
  labs(x = expression(Coefficient~estimate~of~italic("Hindu-Muslim pair")), y = "Subgroup") +
  theme_classic(base_size = 15) 
# note: 0.40197 is the ATE (no controls included)
